package util;

import java.util.StringTokenizer;

/**
 * utility to parse NBG string approaching junction locations are integers
 * 
 * @author Xiaohui
 * 
 */
public class NBGStrParser {
	String state;
	int oid;
	int seqno;
	int timepoint;
	int classid;

	double currPosx;
	double currPosy;
	int speed;
	long roadid;
	int approachingJunctionx, approachingJunctiony;
	StringTokenizer st;

	public NBGStrParser() {
	}

	public NBGStrParser(String line) {
		st = new StringTokenizer(line, "\t");
		state = st.nextToken();
		oid = Integer.parseInt(st.nextToken());
		seqno = Integer.parseInt(st.nextToken());
		// ignore two columns
		classid = Integer.parseInt(st.nextToken());
		timepoint = Integer.parseInt(st.nextToken());
		currPosx = Double.parseDouble(st.nextToken());
		currPosy = Double.parseDouble(st.nextToken());
		speed = (int) Double.parseDouble(st.nextToken());
		roadid = takeRoadId(st.nextToken());
		approachingJunctionx = Integer.parseInt(st.nextToken());
		approachingJunctiony = Integer.parseInt(st.nextToken());
	}

	public void setNBGStr(String NBGStr) {
		st = new StringTokenizer(NBGStr, "\t");
		state = st.nextToken();
		oid = Integer.parseInt(st.nextToken());
		seqno = Integer.parseInt(st.nextToken());
		// ignore two columns
		classid = Integer.parseInt(st.nextToken());
		timepoint = Integer.parseInt(st.nextToken());
		currPosx = Double.parseDouble(st.nextToken());
		currPosy = Double.parseDouble(st.nextToken());
		speed = (int) Double.parseDouble(st.nextToken());
		roadid = takeRoadId(st.nextToken());
		approachingJunctionx = Integer.parseInt(st.nextToken());
		approachingJunctiony = Integer.parseInt(st.nextToken());
	}

	/**
	 * Convert a scientific representation to a long number
	 * 
	 * @param str
	 * @return
	 */
	private long takeRoadId(String str) {
		// System.out.println("input:" +str);
		int idx = str.indexOf("E");
		if (idx == -1) {
			return (long) Double.parseDouble(str);
		}
		Double prefix = Double.parseDouble(str.substring(0, idx));
		int exp = Integer.parseInt(str.substring(idx + 1)); // E+08
		long retval = (long) ((long) Math.pow(10, exp) * prefix);
		return retval;
	}

	/**
	 * @return the state
	 */
	public String getState() {
		return state;
	}

	/**
	 * @return the oid
	 */
	public int getOid() {
		return oid;
	}

	/**
	 * @return the timepoint
	 */
	public int getTimepoint() {
		return timepoint;
	}

	/**
	 * @return the speed
	 */
	public double getSpeed() {
		return speed;
	}

	/**
	 * @return the currPosx
	 */
	public double getCurrPosx() {
		return currPosx;
	}

	/**
	 * @return the currPosy
	 */
	public double getCurrPosy() {
		return currPosy;
	}

	/**
	 * @return the approachingJunctionx
	 */
	public int getApproachingJunctionx() {
		return approachingJunctionx;
	}

	/**
	 * @return the approachingJunctiony
	 */
	public int getApproachingJunctiony() {
		return approachingJunctiony;
	}

	/**
	 * @return the seqno
	 */
	public int getSeqno() {
		return seqno;
	}

	/**
	 * @param seqno
	 *            the seqno to set
	 */
	public void setSeqno(int seqno) {
		this.seqno = seqno;
	}

	/**
	 * @return the classid
	 */
	public int getClassid() {
		return classid;
	}

	/**
	 * @param classid
	 *            the classid to set
	 */
	public void setClassid(int classid) {
		this.classid = classid;
	}

	/**
	 * @return the roadid
	 */
	public long getRoadid() {
		return roadid;
	}

	/**
	 * @param roadid
	 *            the roadid to set
	 */
	public void setRoadid(long roadid) {
		this.roadid = roadid;
	}

}
